package com.chenfan.magic.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenfan.magic.base.R;
import com.chenfan.magic.base.S;
import com.chenfan.magic.dao.IsvMapper;
import com.chenfan.magic.service.IIsvService;
import com.chenfan.magic.entity.Isv;
import com.chenfan.magic.entity.IsvVo;
import com.chenfan.magic.enums.StatusEnum;
import com.chenfan.magic.utils.AES;
import com.chenfan.magic.utils.StringKit;
import com.chenfan.magic.utils.WebKit;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 服务实现类
 *
 * @author troy
 */
@Service
@AllArgsConstructor
public class IsvServiceImpl extends ServiceImpl<IsvMapper, Isv> implements IIsvService {

    public static final int FOUR = 4;

    public static final int FIVE = 5;

    /**
     * 通用线上服务使用的检测 ISV 合法性并获取 ISV
     *
     * @param app_key
     * @return
     */
    @Override
    public R checkIsv(String app_key) {
        S.assets(StringKit.isNotBlank(app_key), S.NOT_EXIST_APP_KEY);
        Isv isv = this.getById(app_key);
        S.assets(isv != null, S.NOT_EXIST_ISV);
        IsvVo isvVo = new IsvVo(isv);
        StatusEnum statusEnum = StatusEnum.getByCode(isv.getStatus());
        if(Objects.nonNull(statusEnum)){
            return S.fail(statusEnum.getDesc())
                    .put("isvInfo", isvVo)
                    .encrypt(isv.getAppSecret(), isv.getAppSecure());
        } else {
            String session_id = WebKit.getParam("session_id");
            String passport_password = WebKit.getParam("passport_password");
            if (StringKit.isNotBlank(passport_password)) {
                S.assets(StringKit.isNotBlank(session_id), S.SESSION_INVALID);
                try {
                    passport_password = AES.decrypt(passport_password, session_id);
                } catch (Exception e) {
                    return S.fail(S.PASSPORT_CHECK_ERROR);
                }
                if (!passport_password.contains(AES.KEY)) {
                    return S.fail(S.PASSPORT_NOT_EQUALS);
                }
                return R.success()
                        .put("isv", isv)
                        .put("isvInfo", isvVo)
                        .put("secret", isv.getAppSecret());
            } else {
                return R.success()
                        .put("isv", isv)
                        .put("isvInfo", isvVo);
            }
        }
    }

    @Override
    public R getInDebug(String appIdOrNamespace) {
        if (StringKit.isBlank(appIdOrNamespace)) {
            return S.fail(S.NOT_EXIST_APP_KEY);
        }
        Isv isv = this.getOne(Wrappers.<Isv>query().lambda().eq(Isv::getAppNamespace, appIdOrNamespace).or().eq(Isv::getAppKey, appIdOrNamespace));
        if (isv == null) {
            return S.fail(S.NOT_EXIST_ISV);
        } else {
            if (FIVE == isv.getStatus()) {
                return S.fail(S.ISV_FORBIDDEN);
            } else if (FOUR == isv.getStatus()) {
                return S.fail(S.ISV_EXC);
            } else {
                return R.success().put(isv);
            }
        }
    }

    @Override
    public R getInProd(String appIdOrNamespace) {
        if (StringKit.isBlank(appIdOrNamespace)) {
            return S.fail(S.NOT_EXIST_APP_KEY);
        }
        Isv isv = this.getOne(Wrappers.<Isv>query().lambda().eq(Isv::getAppNamespace, appIdOrNamespace).or().eq(Isv::getAppKey, appIdOrNamespace));
        if (isv == null) {
            return S.fail(S.NOT_EXIST_ISV);
        } else {
            StatusEnum statusEnum = StatusEnum.getByCode(isv.getStatus());
            if(Objects.nonNull(statusEnum)){
                return S.fail(statusEnum.getDesc());
            } else {
                return R.success().put(isv);
            }
        }
    }
}
